package dymnd.ui;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class PDialog extends JDialog implements ActionListener	{
	public boolean canceled = true;
	
	
	public PDialog(JComponent[] components)	{
		
		Container c = new Container(); //make a new container to store components
		//GridLayout layout = new GridLayout(2, 1);
		setSize(200, 200);
		
		//SpringLayout containerLayout = new SpringLayout(); 
		c.setLayout(new DialogLayout()); // set the layout
		
		setLayout(new GridLayout(2, 1)); // set the dialog's layout
		
		for(int i = 0; i < components.length; i++)	{	
			if(components[i] instanceof JTextField)
				((JTextField)components[i]).addActionListener(this);
			c.add(components[i]);
		}
		
		
		
		int buttonHeight = 20;
		c.setSize(getWidth(), getHeight() - 20);
		c.setLocation(0, 0);
		add(c); // add the container to the jdialog
		
		//add an okay button at the bottom of the dialog
		JButton doneButton = new JButton("Okay");
		doneButton.setSize(50, buttonHeight);
		doneButton.setLocation(getWidth() /2 - doneButton.getWidth() / 2, getHeight() - buttonHeight);
		doneButton.addActionListener(this);
		add(doneButton);
		
		setModal(true);
	}
	
	
	
	public void actionPerformed(ActionEvent e)	{
		//let the API user know that the dialog wasn't canceled
		canceled = false;
		//close the dialog
		setVisible(false);
	}
	
}


class DialogLayout implements LayoutManager	{
	
	public DialogLayout()	{
		
	}
	
	public void layoutContainer(Container c)	{
		
		Component[] components = c.getComponents();
		
		int labelX = 5;
		int y = 10;
		int labelWidth = 50;
		int height = 20;
		int componentWidth = 150;
		for(int i = 0; i < components.length; i++)	{
			
			if(components[i] instanceof JLabel)	{
				//System.out.println("Definitely an instance..");
					if(components.length - (i+1) >= 1 && components[i+1] instanceof JComboBox)	{
						components[i].setLocation(labelX, y);
						components[i].setSize(labelWidth+20, height);
					}	else	{
						components[i].setLocation(labelX, y);
						components[i].setSize(labelWidth, height);
						//System.out.println("I've been called!");
					}
			}
			else if(components[i] instanceof JComboBox)	{
				components[i].setLocation(components[i - 1].getWidth(), y);
				//components[i].setSize(c.getWidth() - components[i -1].getWidth() - components[i -1].getX(), height); 
				components[i].setSize(100, height);
				y += height + 5;
			}
			else {
				
				components[i].setSize(componentWidth, height);
				components[i].setLocation(labelWidth+labelX, y);
				y += height + 5;
				//c.add(components[i]);
				
			}
			
		}
		
	}
	
	public void addLayoutComponent(String s, Component c)	{
		
	}
	
	public void removeLayoutComponent(Component c)	{
		
	}
	
	public Dimension minimumLayoutSize(Container c)	{
		return new Dimension(200, 200);
	}
	
	public Dimension preferredLayoutSize(Container c)	{
		return new Dimension(200, 200);
	}
	
}